package zdc {
/**
 * 集合类工具
 * @author Simple Slime
 */
public class CollectionUtil {
    /**
     * 集合是否包含值
     * @param    coll
     * @param    value
     * @return
     */
    public static function contains(coll:Object, value:Object):Boolean {
        return (coll.indexOf(value) > -1);
    }

    /**
     * 获取等于值或者属性等于值的项
     * @param    coll
     * @param    value
     * @param    prop
     * @return
     */
    public static function getItem(coll:Object, value:Object, prop:String = ""):Object {
        for (var i:int = 0; i < coll.length; i++) {
            var item:Object = coll[i];
            if (prop == "") {
                if (value == item) {
                    return item;
                }
            }
            else {
                if (value == item[prop]) {
                    return item;
                }
            }
        }
        return null;
    }

    /**
     * 删除等于值或属性等于值的项
     * @param    coll
     * @param    value
     * @param    prop
     * @param    num        删除个数
     * @return
     */
    public static function removeItems(coll:Object, value:Object, prop:String = "", num:uint = 0):void {
        var inds:Vector.<uint> = new Vector.<uint>();
        var i:int = 0;
        for (i = 0; i < coll.length; i++) {
            var item:Object = coll[i];
            if (prop == "") {
                if (value == item) {
                    inds.push(i);
                }
            }
            else {
                if (value == item[prop]) {
                    inds.push(i);
                }
            }
        }

        var removes:int = (num == 0 ? inds.length : (inds.length > num ? num : inds.length));
        for (i = 0; i < removes; i++) {
            coll.splice(inds[i] - i, 1);
        }
    }

    /**
     * 清除集合的项
     * @param    coll
     */
    public static function clear(coll:Object):void {
        for (var i:int = coll.length - 1; i >= 0; i--) {
            coll.pop();
        }
    }

    /**
     * 从两个集合（array或vector）之间复制数据
     * @param    coll
     * @param    target
     */
    public static function tranfer(coll:Object, target:Object):void {
        for (var i:int = 0; i < target.length; i++) {
            coll[i] = target[i];
        }
    }
}

}